import pandas as pd

'''
1.读取excel文件
2找出开始和结束节点
3从开始节点把所有关系遍历一遍
4从上到下排序
'''
common_dict = {}
end_dict = {}
single_dict = {}
first_dict = {}

relation_dict={}
deep_dict={}
xdict={}
ydict={}



def getRelation():

    list=[]
    f = open("data/structure.txt")  # 返回一个文件对象
    line = f.readline()  # 调用文件的 readline()方法
    while line:
        line = f.readline()  # 调用文件的 readline()方法
        line=line.replace('\n','')
        if line=='':
            continue
        dict = {}
        array=line.split(',',2)
        dict[array[0]]=array[1]
        list.append(dict)
        #print(line)
    f.close()
    return list

#def findFirstElment(list):

def findAllElment(list):
    source_dict={}
    target_dict={}

    global single_dict
    global first_dict
    global common_dict
    global end_dict
    global relation_dict
    global deep_dict
    for dict in list:
        for key in dict:
            if relation_dict.get(key)==None:
                relation_dict[key]=dict[key]
            else:
                relation_dict[key] =relation_dict[key]+','+ dict[key]
            if key==dict[key]:
                single_dict[key] = key
            else:
                source_dict[key] = key
                target_dict[dict[key]] = dict[key]
    for key in source_dict:
        if target_dict.get(key)==None:
            first_dict[key]=key
        else:
            common_dict[key] = key
    for key in target_dict:
        if source_dict.get(key)==None:
            end_dict[key]=key
    deep_dict = {}
    exec_dict = {}
    initdeep=0
    for key in relation_dict:
        if key==relation_dict[key]:
            deep_dict[key]=-1
        else:
            deep_dict[key]=initdeep
    for key in end_dict:
        deep_dict[key] = initdeep
    #print('deep_dict[K]=',deep_dict['K'])
    print('first_dict=',first_dict)
    print('end_dict=',end_dict)
    print('single_dict=',single_dict)
    print('common_dict=',common_dict)
    print('relation_dict=', relation_dict)
    for key in first_dict:
        countDeep(key,initdeep)
    print('deep_dict=', deep_dict)

def countDeep(element,deepvalue):
    global  first_dict
    global end_dict
    global relation_dict
    global deep_dict
    if end_dict.get(element)!=None:
        return
    array=relation_dict[element].split(',')
    for target in array:
        if deepvalue>=deep_dict[target]:
            deep_dict[target]=deepvalue+1
        countDeep(target,deep_dict[target])

def getDirection():
    s_height=500
    s_weight=1000
    global deep_dict
    global xdict
    global ydict
    hdict={}
    reslist = []
    for key in deep_dict:
        xdict[key]=deep_dict[key]+0
        if hdict.get(deep_dict[key])==None:
            hdict[deep_dict[key]]=0
            ydict[key]=0
        else:
            hdict[deep_dict[key]]=hdict[deep_dict[key]]+1
            ydict[key] = hdict[deep_dict[key]]
        #print(key,xdict[key],hdict[deep_dict[key]])
        #print('hdict',hdict)
        #print('xdict', xdict)
        #print('ydict',ydict)
        #print('hdict', len(hdict))
    #开始到最复杂的计算了
    for key in deep_dict:
        x = int((xdict[key] + 1) / (len(hdict)+1) * s_weight)
        y = int((ydict[key] + 1) / (hdict[deep_dict[key]] + 2) *s_height)
        #print('开始计算元素',key,xdict[key],ydict[key],len(hdict),hdict[deep_dict[key]],'x=',str(x),'y=',str(y))
        mdict={}
        mdict['name']=key
        mdict['x']=x
        mdict['y'] =y
        reslist.append(mdict)
    print(reslist)
    return reslist
def getLink(list):
    reslist=[]
    for dict in list:
        for key in dict:
            mdict={}
            mdict['source']=key
            mdict['target']=dict[key]
            reslist.append(mdict)
    return reslist
    print(reslist)
def startup():
    list=getRelation()
    findAllElment(list)
    link=getLink(list)
    #print(list)
    data=getDirection()
    dict={}
    dict['title'] = '关系图谱666'
    dict['data']=data
    dict['link'] = link
    resdict={}
    resdict['retCode']='0000'
    resdict['retMsg'] = '登录成功'
    resdict['retData']=dict
    mres=str(resdict).replace('\'','\"')
    print(mres)
    #writeToFile(mres,'data/structure.json','utf-8')
    path='D:\\workspace\\vuedemo\\public\\static\\structure.json'
    writeToFile(mres, path, 'utf-8')
def writeToFile(res,filename,encodes='gbk'):
    # 打开一个文件
    fo = open(filename, "w",encoding=encodes)
    fo.write(res)
    # 关闭打开的文件
    fo.close()
if __name__ == "__main__":
    startup()